package com.common.base.persistence;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分页类
 * 
 * @author jfeng
 * @version 2014-03-04
 * @param <T>
 */
public class Page<T> implements Serializable{
	
	/**  
	* 字段:      字段名称
	* @Fields serialVersionUID : TODO 
	*/
	private static final long serialVersionUID = 1L;
	private int page; //当前页码
	private int rows; //每页显示记录条数,设置为“-1”表示不进行分页（分页无效）
	private long count;// 总记录数，设置为“-1”表示不查询总数
	private String orderBy = "";
	private boolean success;
	
	private List<T> list = new ArrayList<T>();
	
	private Map<String, Object> dataset; // zui数据格式要求
	
	/**
	 * 分页是否有效
	 * @return this.pageSize==-1
	 */
	public boolean isDisabled() {
		return this.rows==-1;
	}
	
	/**
	 * 是否进行总数统计
	 * @return this.count==-1
	 */
	public boolean isNotCount() {
		return this.count==-1;
	}
	
	/**
	 * 获取 Hibernate FirstResult
	 */
	public int getFirstResult(){
		int firstResult = (getPage() - 1) * getRows();
		if (firstResult >= getCount()) {
			firstResult = 0;
		}
		return firstResult;
	}
	/**
	 * 获取 Hibernate MaxResults
	 */
	public int getMaxResults(){
		return getRows();
	}
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public long getCount() {
		return count;
	}
	public void setCount(long count) {
		this.count = count;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public Map<String, Object> getDataset() {
		return dataset;
	}
	public void setDataset(Map<String, Object> dataset) {
		this.dataset = dataset;
	}
	public void setSuccess(boolean isSuccess) {
		this.success = isSuccess;
	}
	public boolean isSuccess() {
		return this.success;
	}
	
	public void loadData(List<T> list){
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<T> mPage = (com.github.pagehelper.Page<T>) list;
			this.count = mPage.getTotal();
			this.list = mPage.getResult();
		} else {
			this.count = list.size();
			this.list = list;
		}
	}
	
}
